import 'package:get/get.dart';
import 'package:hive_ce_flutter/adapters.dart';
import 'package:vischat/core/service/storage/common_storage.dart';
import 'package:vischat/core/service/storage/secure_auth.dart';
import 'package:vischat/core/service/storage/secure_conversation.dart';

class StorageManager extends GetxService {
  final SecureAuthStorage tokenStorage;
  final SecureConversationStorage conversationStorage;
  final CommonStorage commonStorage;

  static Future<StorageManager> create() async {
    await Hive.initFlutter();
    final results = await Future.wait([
      SecureAuthStorage().init(),
      SecureConversationStorage().init(),
      CommonStorage().init(),
    ]);
    return StorageManager._internal(
      tokenStorage: results[0] as SecureAuthStorage,
      conversationStorage: results[1] as SecureConversationStorage,
      commonStorage: results[2] as CommonStorage,
    );
  }

  StorageManager._internal({
    required this.tokenStorage,
    required this.conversationStorage,
    required this.commonStorage,
  });

  @override
  Future<void> onClose() async {
    await Future.wait([
      tokenStorage.close(),
      conversationStorage.close(),
    ]);
    super.onClose();
  }
}
